package com.zjmzxfzhl.modules.app.service.impl;

import com.zjmzxfzhl.modules.app.entity.AppAlarmSet;
import com.zjmzxfzhl.modules.app.entity.AppDir;
import com.zjmzxfzhl.modules.app.entity.AppProj;
import com.zjmzxfzhl.modules.app.service.AppAlarmUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zjmzxfzhl.common.core.base.BaseServiceImpl;
import com.zjmzxfzhl.modules.app.entity.AppAlarm;
import com.zjmzxfzhl.modules.app.mapper.AppAlarmMapper;
import com.zjmzxfzhl.modules.app.service.AppAlarmService;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * Service
 * 
 * @author 张森荣
 */
@Service
public class AppAlarmServiceImpl extends BaseServiceImpl<AppAlarmMapper, AppAlarm>
        implements AppAlarmService {
    @Autowired
    private AppAlarmUserService appAlarmUserService;

    @Override
    public IPage<AppAlarm> list(IPage<AppAlarm> page, AppAlarm appAlarm) {
        return page.setRecords(baseMapper.list(page, appAlarm));
    }
    public Integer selectRowCount(AppAlarm appAlarm) {
        return baseMapper.selectRowCount(appAlarm);
    }

    @Override
    public  boolean addAlarm(AppAlarmSet appAlarmSet){
        AppAlarm appAlarm=new AppAlarm();
        appAlarm.setAlarmDate(new Date());
        appAlarm.setAlarmType(appAlarmSet.getSetType());
        appAlarm.setAlarmTypeId(appAlarmSet.getSetTypeId());
        appAlarm.setContent(appAlarmSet.getContent());
        appAlarm.setName(appAlarmSet.getName());
        appAlarm.setSetId(appAlarmSet.getSetId());
        appAlarm.setStatus("00");
        //提醒人员
        String userIds=appAlarmUserService.getAlarmSetUser(appAlarmSet.getSetId());
        appAlarm.setUserIds(userIds);
        Boolean result=this.save(appAlarm);
        return result;
    }

    /**
     * 已执行
     *
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean verified(String alarmId) {
        AppAlarm appAlarm=this.getById(alarmId);
        if(appAlarm==null){
            return false;
        }
        appAlarm.setStatus("01");
        appAlarm.setUpdateTime(new Date());
        Boolean result = this.updateById(appAlarm);

        return result;
    }

    /**
     * 关闭的
     *
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean close(String alarmId) {
        AppAlarm appAlarm=this.getById(alarmId);
        if(appAlarm==null){
            return false;
        }
        appAlarm.setStatus("02");
        appAlarm.setUpdateTime(new Date());
        Boolean result = this.updateById(appAlarm);

        return result;
    }

    /**
     * 待办的
     *
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean wait(String alarmId) {
        AppAlarm appAlarm=this.getById(alarmId);
        if(appAlarm==null){
            return false;
        }
        appAlarm.setStatus("00");
        appAlarm.setUpdateTime(new Date());
        Boolean result = this.updateById(appAlarm);

        return result;
    }
}
